﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleAppThread
{
	public class IniReader
	{
		private MyMultiMap<string, string> m_container;
		private Dictionary<string, Dictionary<string, string>> m_dictContainer;

		protected StreamReader m_streamReader;
		protected string m_fileName;
		string m_data = string.Empty;

		public IniReader()
		{
			m_container = new MyMultiMap<string, string>();
			m_dictContainer = new Dictionary<string, Dictionary<string, string>>();
			m_fileName = string.Empty;
		}

		public IniReader(string filePath)
		{
			m_container = new MyMultiMap<string, string>();
			m_dictContainer = new Dictionary<string, Dictionary<string, string>>();
			m_fileName = filePath;
		}

		public void Load()
		{
			if (!File.Exists(m_fileName))
			{
				using (StreamWriter sw = new StreamWriter(m_fileName))
				{
					sw.WriteLine("//Ini file settings");
					sw.WriteLine("[Data]");
					sw.WriteLine("Value1=Data1");
					sw.WriteLine("Value2=Data2");
					sw.WriteLine("Value3=Data3");
				}

			}
			m_streamReader = new StreamReader(this.m_fileName);

			this.m_dictContainer.Clear();
			this.m_container.ClearAll();

			string data = string.Empty;
			string key = string.Empty;

			while ((data = m_streamReader.ReadLine()) != null)
			{
				if (data.StartsWith("//") || data.StartsWith("\\") || data.StartsWith("--") || string.IsNullOrEmpty(data))
					continue;

				if (data.StartsWith("[") && data.EndsWith("]") && data.Length > 2)
				{
					key = data.Replace("[", "").Replace("]", "").Trim();
					m_container.AddWithoutValue(key);
					
					m_dictContainer.Add(key,new Dictionary<string,string>());
					continue;
				}
				else
				{
					int i;
					if ((i = data.IndexOf('=')) > 0)
					{
						string par = data.Substring(0, i);
						int j = data.Length - 1 - i;
						string val = j > 0 ? data.Substring(i + 1, j) : "";

						if (m_dictContainer.ContainsKey(key))
						{
							m_dictContainer[key].Add(par, val);
						}
		
						m_container.Add(key, data.Trim());
					}
				}
			}

			m_streamReader.Close();
		}

		public MyMultiMap<string, string> Data
		{
			get
			{
				return this.m_container;
			}

			private set
			{
				m_container = value;
			}
		}

		public Dictionary<string, Dictionary<string, string>> Data2
		{
			get
			{
				return m_dictContainer;
			}
		}


		public List<string> GetKeys()
		{
			if (m_container.Count > 0)
			{
				List<string> list = new List<string>();
				foreach (KeyValuePair<string, List<string>> item in m_container)
				{
					list.Add(item.Key);
				}
				return list;
			}

			return new List<string>();
		}

		public List<string> GetValuesForKey(string key)
		{
			bool finded = false;

			if (m_container.Count > 0)
			{
				List<string> list = new List<string>();
				foreach (KeyValuePair<string, List<string>> item in m_container)
				{
					if (item.Key.ToLower() == key.ToLower())
					{
						finded = true;
						foreach (string s in item.Value)
						{
							list.Add(s);
						}
					}

					if (finded)
						break;
				}
				return list;
			}

			return new List<string>();
		}

	}
}
